package com.hongyuan.venue.ui.person.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.hongyuan.venue.R;
import com.hongyuan.venue.base.AppViewModelFactory;
import com.hongyuan.venue.base.CustomFragment;
import com.hongyuan.venue.data.Constants;
import com.hongyuan.venue.databinding.FragmentMyOrdersBinding;
import com.hongyuan.venue.ui.person.view_mode.MyOrdersViewModel;
import com.hongyuan.venue.utils.CustomDialog;

import me.tatarka.bindingcollectionadapter2.BR;

public class MyOrdersFragment extends CustomFragment<FragmentMyOrdersBinding, MyOrdersViewModel> {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_my_orders;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public MyOrdersViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(MyOrdersViewModel.class);
    }

    @Override
    public void initView() {
        super.initView();
        setOnRefresh(binding.refresh,REFRESH_0X3);
        setEnableLoadMore(Constants.GYM_ORDER_LIST);
        setEnableRefresh(Constants.GYM_ORDER_LIST);
    }

    @Override
    public void initData() {
        super.initData();

        viewModel.clearParams().setParams("o_state",getPosition());
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.cancelOrder.observe(this, aVoid -> {
            CustomDialog.promptDialog(getContext(), "确定要取消订单？", "确定！", "不确定？", false, v -> {
                if(v.getId() == R.id.isOk){
                    viewModel.setParams("o_id",String.valueOf(viewModel.itemViewModel.entity.get().getO_id()));
                    viewModel.requestData(Constants.CANCEL_ORDER);
                }
            });
        });
    }
}
